using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Services.Protocols;
using Castle.DynamicProxy;
using Sio.Mdm.Server;
using Castle.Core.Interceptor;

namespace Sio.Mdm.Server
{
    class WebServiceProxy : SoapHttpClientProtocol, IService, IInterceptor
    {
        #region Fields
        private ServiceInformation Service;
        #endregion

        #region Construction
        public WebServiceProxy(ServiceInformation service)
        {
            Service = service;            
            Url = service.Address;            
        }
        #endregion

        #region IInterceptor Members
        /// <summary>
        /// Route calls to web service
        /// </summary>
        /// <param name="invocation">Information on invocation</param>
        /// <param name="args">Argument to pass to function</param>
        /// <returns>Result of calling function</returns>
        private object Intercept(IInvocation invocation, params object[] args)
        {
            object []results = this.Invoke(invocation.Method.Name, args);
            return results.Length > 0 ? results[0] : null;
        }
        #endregion

        #region IInterceptor Members

        void IInterceptor.Intercept(IInvocation invocation)
        {
            invocation.ReturnValue = Intercept(invocation, invocation.Arguments);
        }

        #endregion
    }
}
